{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9e616174-63d1-4ee0-a8a6-ce8cb90014e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from pandasql import sqldf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3d1549b0-73af-4d86-b9c8-e218051d1b33",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>地域</th>\n",
       "      <th>省份</th>\n",
       "      <th>产品年</th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "      <th>市值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>甲</td>\n",
       "      <td>2020</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>甲</td>\n",
       "      <td>2021</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>乙</td>\n",
       "      <td>2022</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>B</td>\n",
       "      <td>丙</td>\n",
       "      <td>2020</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>B</td>\n",
       "      <td>丁</td>\n",
       "      <td>2021</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>B</td>\n",
       "      <td>丁</td>\n",
       "      <td>2022</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>C</td>\n",
       "      <td>戊</td>\n",
       "      <td>2020</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>C</td>\n",
       "      <td>己</td>\n",
       "      <td>2021</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>C</td>\n",
       "      <td>己</td>\n",
       "      <td>2022</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  地域 省份   产品年  数量  金额  市值\n",
       "0  A  甲  2020   1  10  15\n",
       "1  A  甲  2021   2  20  30\n",
       "2  A  乙  2022   3  30  45\n",
       "3  B  丙  2020   1  10  15\n",
       "4  B  丁  2021   2  20  30\n",
       "5  B  丁  2022   3  30  45\n",
       "6  C  戊  2020   1  10  15\n",
       "7  C  己  2021   2  20  30\n",
       "8  C  己  2022   3  30  45"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_excel('./test.xlsx',sheet_name='Sheet1')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dc553fe5-2c6d-420c-9e13-5ff03b0b6a20",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>地域</th>\n",
       "      <th>省份</th>\n",
       "      <th>2020 数量</th>\n",
       "      <th>2021 数量</th>\n",
       "      <th>2022 数量</th>\n",
       "      <th>总计 数量</th>\n",
       "      <th>2020 金额</th>\n",
       "      <th>2021 金额</th>\n",
       "      <th>2022 金额</th>\n",
       "      <th>总计 金额</th>\n",
       "      <th>2020 市值</th>\n",
       "      <th>2021 市值</th>\n",
       "      <th>2022 市值</th>\n",
       "      <th>总计 市值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>乙</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>30</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>45</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>甲</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>10</td>\n",
       "      <td>20</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>15</td>\n",
       "      <td>30</td>\n",
       "      <td>0</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>B</td>\n",
       "      <td>丁</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>B</td>\n",
       "      <td>丙</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>C</td>\n",
       "      <td>己</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>C</td>\n",
       "      <td>戊</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>小计</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>60</td>\n",
       "      <td>15</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>B</td>\n",
       "      <td>小计</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>60</td>\n",
       "      <td>15</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>C</td>\n",
       "      <td>小计</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>60</td>\n",
       "      <td>15</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>总计</td>\n",
       "      <td></td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>9</td>\n",
       "      <td>18</td>\n",
       "      <td>30</td>\n",
       "      <td>60</td>\n",
       "      <td>90</td>\n",
       "      <td>180</td>\n",
       "      <td>45</td>\n",
       "      <td>90</td>\n",
       "      <td>135</td>\n",
       "      <td>270</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   地域  省份  2020 数量  2021 数量  2022 数量  总计 数量  2020 金额  2021 金额  2022 金额  总计 金额  \\\n",
       "0   A   乙        0        0        3      3        0        0       30     30   \n",
       "1   A   甲        1        2        0      3       10       20        0     30   \n",
       "2   B   丁        0        2        3      5        0       20       30     50   \n",
       "3   B   丙        1        0        0      1       10        0        0     10   \n",
       "4   C   己        0        2        3      5        0       20       30     50   \n",
       "5   C   戊        1        0        0      1       10        0        0     10   \n",
       "0   A  小计        1        2        3      6       10       20       30     60   \n",
       "1   B  小计        1        2        3      6       10       20       30     60   \n",
       "2   C  小计        1        2        3      6       10       20       30     60   \n",
       "0  总计            3        6        9     18       30       60       90    180   \n",
       "\n",
       "   2020 市值  2021 市值  2022 市值  总计 市值  \n",
       "0        0        0       45     45  \n",
       "1       15       30        0     45  \n",
       "2        0       30       45     75  \n",
       "3       15        0        0     15  \n",
       "4        0       30       45     75  \n",
       "5       15        0        0     15  \n",
       "0       15       30       45     90  \n",
       "1       15       30       45     90  \n",
       "2       15       30       45     90  \n",
       "0       45       90      135    270  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pandasql 的底层是基于 SQLite 的\n",
    "\n",
    "pyqldf = lambda q: sqldf(q, globals())\n",
    "\n",
    "query1 = \"\"\"\n",
    "        SELECT\n",
    "        df.\"地域\",\n",
    "        df.\"省份\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2020 THEN df.\"数量\"  ELSE 0 END ) AS \"2020 数量\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2021 THEN df.\"数量\"  ELSE 0 END ) AS \"2021 数量\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2022 THEN df.\"数量\"  ELSE 0 END ) AS \"2022 数量\",\n",
    "        SUM (df.\"数量\" ) AS \"总计 数量\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2020 THEN df.\"金额\"  ELSE 0 END ) AS \"2020 金额\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2021 THEN df.\"金额\"  ELSE 0 END ) AS \"2021 金额\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2022 THEN df.\"金额\"  ELSE 0 END ) AS \"2022 金额\",\n",
    "        SUM (df.\"金额\" ) AS \"总计 金额\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2020 THEN df.\"市值\"  ELSE 0 END ) AS \"2020 市值\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2021 THEN df.\"市值\"  ELSE 0 END ) AS \"2021 市值\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2022 THEN df.\"市值\"  ELSE 0 END ) AS \"2022 市值\", \n",
    "        SUM (df.\"市值\" ) AS \"总计 市值\"\n",
    "        FROM\n",
    "        df \n",
    "        GROUP BY\n",
    "        df.\"地域\", df.\"省份\"\n",
    "        ORDER BY\n",
    "        df.\"地域\",\n",
    "        df.\"省份\"\n",
    "        ;\n",
    "        \"\"\"\n",
    "\n",
    "query2 = \"\"\"\n",
    "        SELECT\n",
    "        df.\"地域\",\n",
    "        '小计' AS \"省份\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2020 THEN df.\"数量\"  ELSE 0 END ) AS \"2020 数量\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2021 THEN df.\"数量\"  ELSE 0 END ) AS \"2021 数量\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2022 THEN df.\"数量\"  ELSE 0 END ) AS \"2022 数量\",\n",
    "        SUM (df.\"数量\" ) AS \"总计 数量\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2020 THEN df.\"金额\"  ELSE 0 END ) AS \"2020 金额\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2021 THEN df.\"金额\"  ELSE 0 END ) AS \"2021 金额\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2022 THEN df.\"金额\"  ELSE 0 END ) AS \"2022 金额\",\n",
    "        SUM (df.\"金额\" ) AS \"总计 金额\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2020 THEN df.\"市值\"  ELSE 0 END ) AS \"2020 市值\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2021 THEN df.\"市值\"  ELSE 0 END ) AS \"2021 市值\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2022 THEN df.\"市值\"  ELSE 0 END ) AS \"2022 市值\", \n",
    "        SUM (df.\"市值\" ) AS \"总计 市值\"\n",
    "        FROM\n",
    "        df \n",
    "        GROUP BY\n",
    "        df.\"地域\"\n",
    "        ORDER BY\n",
    "        df.\"地域\"\n",
    "        ;\n",
    "        \"\"\"\n",
    "\n",
    "query3 = \"\"\"\n",
    "        SELECT\n",
    "        '总计' AS \"地域\",\n",
    "        '' AS \"省份\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2020 THEN df.\"数量\"  ELSE 0 END ) AS \"2020 数量\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2021 THEN df.\"数量\"  ELSE 0 END ) AS \"2021 数量\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2022 THEN df.\"数量\"  ELSE 0 END ) AS \"2022 数量\",\n",
    "        SUM (df.\"数量\" ) AS \"总计 数量\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2020 THEN df.\"金额\"  ELSE 0 END ) AS \"2020 金额\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2021 THEN df.\"金额\"  ELSE 0 END ) AS \"2021 金额\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2022 THEN df.\"金额\"  ELSE 0 END ) AS \"2022 金额\",\n",
    "        SUM (df.\"金额\" ) AS \"总计 金额\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2020 THEN df.\"市值\"  ELSE 0 END ) AS \"2020 市值\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2021 THEN df.\"市值\"  ELSE 0 END ) AS \"2021 市值\",\n",
    "        SUM ( CASE WHEN df.\"产品年\"  = 2022 THEN df.\"市值\"  ELSE 0 END ) AS \"2022 市值\",\n",
    "        SUM (df.\"市值\" ) AS \"总计 市值\"\n",
    "        FROM\n",
    "        df\n",
    "        ;\n",
    "        \"\"\"\n",
    "\n",
    "# 曲线救国，代替 UNION 的功能\n",
    "result1 = pyqldf(query1)\n",
    "result2 = pyqldf(query2)\n",
    "result3 = pyqldf(query3)\n",
    "result = pd.concat([result1,result2,result3])\n",
    "result"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
